#include "dev.h"
#include "gpio.h"
#include "kernel.h"
#include "iodev.h"
#include <string.h>
#include "ioctl.h"
#include "regs.h"
#include "config.h"

#if 1
void lcd_q03a_init(uint32_t cmd_addr, uint32_t data_addr)
{
	//CS0=0;
	
	/*RST=1;  
	mdelay(100);
	
	RST=0;
	mdelay(800);

	RST=1;
	mdelay(800);*/


  //************* Start Initial Sequence **********//
//HX8368_231

  mdelay(120);
  writeb(cmd_addr, 0x11); // Sleep Out
  mdelay(150);
  writeb(cmd_addr, 0xB9); // Set EXTC
  writeb(data_addr, 0xFF);
  writeb(data_addr, 0x83);
  writeb(data_addr, 0x68);
  mdelay(5);
  writeb(cmd_addr, 0x36); // Set MADCTL
  writeb(data_addr, 0xC8);
  writeb(cmd_addr, 0x3A); // Set CSEL
  writeb(data_addr, 0x05); // For 65K color
  
  writeb(cmd_addr, 0xB5); // Himax internal use
  writeb(data_addr, 0x07);
  writeb(data_addr, 0x07);
  mdelay(5);
  writeb(cmd_addr, 0xBD); // Himax internal use
  writeb(data_addr, 0x00);
  writeb(data_addr, 0x20);
  writeb(data_addr, 0x52);
  mdelay(5);
  writeb(cmd_addr, 0xC2); // Himax internal use
  writeb(data_addr, 0x00);
  writeb(data_addr, 0x30);
  mdelay(5);
  writeb(cmd_addr, 0xC0); // Himax internal use
  writeb(data_addr, 0x23);
  writeb(data_addr, 0x05);


  writeb(data_addr, 0x08);
  writeb(data_addr, 0xF2);
  mdelay(5);
  writeb(cmd_addr, 0xE3); // Himax internal use
  writeb(data_addr, 0x00);
  writeb(data_addr, 0x5F);
  writeb(data_addr, 0x00);
  writeb(data_addr, 0x5F);
  mdelay(5);
  writeb(cmd_addr, 0xB0); // Set OSC
  writeb(data_addr, 0x0D); // Change the frame rate to 75Hz
  mdelay(5);
  writeb(cmd_addr, 0xB1); // Set Power
  writeb(data_addr, 0x00);
  writeb(data_addr, 0x02);
  writeb(data_addr, 0x1E);
  writeb(data_addr, 0x04);
  writeb(data_addr, 0x24);
  writeb(data_addr, 0x14);
  writeb(data_addr, 0xD4);
  mdelay(5);
  writeb(cmd_addr, 0xB6); // Set VCOM
  writeb(data_addr, 0x71);
  writeb(data_addr, 0x93);
  writeb(data_addr, 0x74);
  mdelay(5);
  writeb(cmd_addr, 0xE0); // Set Gamma
  writeb(data_addr, 0x17);
  writeb(data_addr, 0x29);
  writeb(data_addr, 0x26);
  writeb(data_addr, 0x27);
  writeb(data_addr, 0x21);
  writeb(data_addr, 0x3F);
  writeb(data_addr, 0x1D);
  writeb(data_addr, 0x6C);
  writeb(data_addr, 0x09);
  writeb(data_addr, 0x03);
  writeb(data_addr, 0x04);
  writeb(data_addr, 0x0A);
  writeb(data_addr, 0x17);
  writeb(data_addr, 0x00);
  writeb(data_addr, 0x1E);
  writeb(data_addr, 0x18);
  writeb(data_addr, 0x19);
  writeb(data_addr, 0x16);
  writeb(data_addr, 0x20);
  writeb(data_addr, 0x13);
  writeb(data_addr, 0x62);
  writeb(data_addr, 0x08);
  writeb(data_addr, 0x15);
  writeb(data_addr, 0x1B);
  writeb(data_addr, 0x1C);
  writeb(data_addr, 0x16);
  writeb(data_addr, 0xFF);

  writeb(cmd_addr, 0x29);//display on
   mdelay(50);

  writeb(cmd_addr, 0x2C);
}
#else
void lcd_q03a_init(uint32_t cmd_addr, uint32_t data_addr)
{
  // VCI=2.8V

  //************* Start Initial Sequence **********//
  mdelay(120);
  writeb(cmd_addr,0xC8);       //Set EXTC
  writeb(data_addr,0xFF);
  writeb(data_addr,0x93);
  writeb(data_addr,0x42);
  
  writeb(cmd_addr,0x36);       //Memory Access Control
  writeb(data_addr,0xC8); //MY,MX,MV,ML,BGR,MH
  writeb(cmd_addr,0x3A);       //Pixel Format Set
  writeb(data_addr,0x55); //DPI [2:0],DBI [2:0]
  
  writeb(cmd_addr,0xC0);       //Power Control 1
  writeb(data_addr,0x13); //VRH[5:0]
  writeb(data_addr,0x0E); //VC[3:0]
  
  writeb(cmd_addr,0xC1);       //Power Control 2
  writeb(data_addr,0x02); //SAP[2:0],BT[3:0]
  
  writeb(cmd_addr,0xC5);       //VCOM
  writeb(data_addr,0xD3);
  
  writeb(cmd_addr,0xB1);      
  writeb(data_addr,0x00);     
  writeb(data_addr,0x1B);
  writeb(cmd_addr,0xB4);      
  writeb(data_addr,0x02);
  
  writeb(cmd_addr,0xE0);
  writeb(data_addr,0x00);//P01-VP63   
  writeb(data_addr,0x01);//P02-VP62   
  writeb(data_addr,0x04);//P03-VP61   
  writeb(data_addr,0x00);//P04-VP59   
  writeb(data_addr,0x11);//P05-VP57   
  writeb(data_addr,0x08);//P06-VP50   
  writeb(data_addr,0x35);//P07-VP43   
  writeb(data_addr,0x79);//P08-VP27,36
  writeb(data_addr,0x45);//P09-VP20   
  writeb(data_addr,0x07);//P10-VP13   
  writeb(data_addr,0x0D);//P11-VP6    
  writeb(data_addr,0x09);//P12-VP4    
  writeb(data_addr,0x16);//P13-VP2    
  writeb(data_addr,0x17);//P14-VP1    
  writeb(data_addr,0x0F);//P15-VP0    
  
  writeb(cmd_addr,0xE1);
  writeb(data_addr,0x00);//P01
  writeb(data_addr,0x28);//P02
  writeb(data_addr,0x29);//P03
  writeb(data_addr,0x02);//P04
  writeb(data_addr,0x0F);//P05
  writeb(data_addr,0x06);//P06
  writeb(data_addr,0x3F);//P07
  writeb(data_addr,0x25);//P08
  writeb(data_addr,0x55);//P09
  writeb(data_addr,0x06);//P10
  writeb(data_addr,0x15);//P11
  writeb(data_addr,0x0F);//P12
  writeb(data_addr,0x38);//P13
  writeb(data_addr,0x38);//P14
  writeb(data_addr,0x0F);//P15
  
  writeb(cmd_addr,0x11);//Exit Sleep
  mdelay(120);
  writeb(cmd_addr,0x29);//Display On

}
#endif
